package com.jspgou.core.web.front;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.ui.ModelMap;

import com.jspgou.cms.entity.Store;
import com.jspgou.common.web.RequestUtils;
import com.jspgou.common.web.springmvc.MessageResolver;
import com.jspgou.core.entity.Website;
import com.jspgou.core.web.WebErrors;

public abstract class FrontHelper{
	
    public static final String TPL_PAGE_NOT_FOUND = "tpl.pageNotFound";
    public static final String TPL_SUCCESS_PAGE = "tpl.successPage";
    public static final String TPL_ERROR_PAGE = "tpl.errorPage";
    public static final String TPL_MESSAGE_PAGE = "tpl.messagePage";

    public static String pageNotFound(Website website, ModelMap modelmap, 
    		HttpServletRequest request){
        setCommonData(request, modelmap, website);
        return website.getTplSys("common", MessageResolver.getMessage(request, TPL_PAGE_NOT_FOUND, new Object[0]), request);
    }

    public static String showSuccess(String message, String backUrl, Website website, 
    		ModelMap modelmap, HttpServletRequest request) {
        setCommonData(request, modelmap, website);
        modelmap.addAttribute("message", message);
        if(!StringUtils.isBlank(backUrl))
            modelmap.addAttribute("backUrl", backUrl);
        return website.getTplSys("common", MessageResolver.getMessage(request, TPL_SUCCESS_PAGE, new Object[0]), request);
    }

	public static String showError(WebErrors weberrors, Website website, 
    		ModelMap modelmap, HttpServletRequest request){
        setCommonData(request, modelmap, website);
        weberrors.toModel(modelmap);
        return website.getTplSys("common", MessageResolver.getMessage(request,TPL_ERROR_PAGE, new Object[0]), request);
    }

    public static String showMessage(String s, Website website,
    		ModelMap modelmap, HttpServletRequest request){
        setCommonData(request, modelmap, website);
        modelmap.addAttribute("message", s);
        return website.getTplSys("common", MessageResolver.getMessage(request, TPL_MESSAGE_PAGE, new Object[0]), request);
    }

    public static void setDynamicPageData(HttpServletRequest request, ModelMap model, 
    		Website web, String location, String urlPrefix, String urlSuffix, int pageNo){
    	model.addAttribute("urlPrefix", urlPrefix);
    	model.addAttribute("urlSuffix", urlSuffix);
        setCommonData(request, model, web, location);
    }
    
    public static void setCommonData(HttpServletRequest request, 
    		ModelMap modelmap, Website website){
    	String location=RequestUtils.getLocation(request);
        setCommonData(request, modelmap, website,location);
    }

    private static void setCommonData(HttpServletRequest request, ModelMap modelmap, 
    		Website website, String location){
        modelmap.addAttribute("location", location);
        modelmap.addAttribute("web", website);
        String baseDomain = (String)request.getAttribute("_base_domain_key");
        if(baseDomain != null){
            modelmap.addAttribute("baseDomain", baseDomain);
        }
        String loginUrl = (String)request.getAttribute("_login_url");
        if(loginUrl != null){
            modelmap.addAttribute("loginUrl", getLoginUrl(loginUrl, request.getContextPath(), location));
        }
        //前台页面引用路径
        modelmap.addAttribute("root", website.getResBaseUrl(request));
        modelmap.addAttribute("sysResRoot", website.getFrontResUrl());
        modelmap.addAttribute("_start_time", request.getAttribute("_start_time"));
        //上传图片路径-2013-01-15
		String uploadRoot=website.getUploadUrl();
		modelmap.addAttribute("uploadRoot", uploadRoot);
    }
    
    public static void setStoreCommonData(HttpServletRequest request, 
    		ModelMap modelmap, Website website,Store store){
        modelmap.addAttribute("storeRoot", website.getStoreResBaseUrl(store.getStorePath(), request));
        modelmap.addAttribute("store", store);
    }

    public static String getLoginUrl(String s, String s1, String s2){
        StringBuilder stringbuilder = new StringBuilder();
        if(!s.startsWith("http")){
            stringbuilder.append(s1);
            if(!s.startsWith("/")){
                stringbuilder.append("/");
            }
        }
        stringbuilder.append(s).append("?returnUrl=").append(s2);
        return stringbuilder.toString();
    }

    public static String getLoginUrl(HttpServletRequest request){
        return getLoginUrl((String)request.getAttribute("_login_url"), "", request.getRequestURL().toString());
    }
}
